home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / LOCALM~1 / RegTool.bas < prev    next >
BASIC Source File  |  1997-06-14  |  12KB  |  348 lines

  1. Attribute VB_Name = "MRegTool"
  2. Option Explicit
  3.  
  4. Public Enum EErrorRegTool
  5.     eeBaseRegTool = 13590   ' RegTool
  6. End Enum
  7.  
  8. Const sWin = "Software\Microsoft\Windows\"
  9. Const sExp = "CurrentVersion\Explorer\Shell Folders"
  10. Const sWinExp = sWin & sExp
  11. Const sBack = "\"
  12.  
  13. Function GetRegValue(ByVal hKey As Long, sName As String, _
  14.                      vValue As Variant) As Long
  15.     Dim cData As Long, sData As String, ordType As Long, e As Long
  16.     e = RegQueryValueEx(hKey, sName, pNull, ordType, 0&, cData)
  17.     If e And e <> ERROR_MORE_DATA Then Exit Function
  18.     Select Case ordType
  19.     Case REG_DWORD, REG_DWORD_LITTLE_ENDIAN
  20.         Dim iData As Long
  21.         e = RegQueryValueExInt(hKey, sName, pNull, _
  22.                                ordType, iData, cData)
  23.         vValue = iData
  24.         
  25.     Case REG_DWORD_BIG_ENDIAN  ' Unlikely, but you never know
  26.         Dim dwData As Long
  27.         e = RegQueryValueExInt(hKey, sName, pNull, _
  28.                                ordType, dwData, cData)
  29.         vValue = MBytes.SwapEndian(dwData)
  30.         
  31.     Case REG_SZ, REG_MULTI_SZ ' Same thing to Visual Basic
  32.         sData = String$(cData - 1, 0)
  33.         e = RegQueryValueExStr(hKey, sName, pNull, _
  34.                                ordType, sData, cData)
  35.         vValue = sData
  36.         
  37.     Case REG_EXPAND_SZ
  38.         sData = String$(cData - 1, 0)
  39.         e = RegQueryValueExStr(hKey, sName, pNull, _
  40.                                ordType, sData, cData)
  41.         vValue = MUtility.ExpandEnvStr(sData)
  42.         
  43.     ' Catch REG_BINARY and anything else
  44.     Case Else
  45.         Dim abData() As Byte
  46.         ReDim abData(cData)
  47.         e = RegQueryValueExByte(hKey, sName, pNull, _
  48.                                 ordType, abData(0), cData)
  49.         vValue = abData
  50.         
  51.     End Select
  52.     GetRegValue = e
  53. End Function
  54.  
  55. Function CreateRegValue(vValueA As Variant, ByVal hKeyA As Long, _
  56.                         Optional sNameA As String) As Long
  57.     Dim c As Long, e As Long, ordType As Long
  58.     Select Case VarType(vValueA)
  59.     Case vbArray + vbByte
  60.         Dim ab() As Byte
  61.         ab = vValueA
  62.         ordType = REG_BINARY
  63.         c = UBound(ab) - LBound(ab) - 1
  64.         e = RegSetValueExByte(hKeyA, sNameA, pNull, ordType, ab(0), c)
  65.         
  66.     Case vbLong, vbInteger
  67.         Dim i As Long
  68.         i = vValueA
  69.         ordType = REG_DWORD
  70.         e = RegSetValueExInt(hKeyA, sNameA, pNull, ordType, i, 4)
  71.         
  72.     Case vbString
  73.         Dim s As String, iPos As Long
  74.         s = vValueA
  75.         ordType = REG_SZ
  76.         ' Assume anything with two non-adjacent percents is expanded string
  77.         iPos = InStr(s, "%")
  78.         If iPos Then
  79.             If InStr(iPos + 2, s, "%") Then ordType = REG_EXPAND_SZ
  80.         End If
  81.         c = Len(s) + 1
  82.         e = RegSetValueExStr(hKeyA, sNameA, pNull, ordType, s, c)
  83.         
  84.     ' User should convert to a compatible type before calling
  85.     Case Else
  86.         e = ERROR_INVALID_DATA
  87.         
  88.     End Select
  89.     CreateRegValue = e
  90. End Function
  91.  
  92. Function GetRegValueNext(ByVal hKey As Long, _
  93.                          i As Long, _
  94.                          sName As String, _
  95.                          vValue As Variant) As Long
  96.     Dim cName As Long, cData As Long, sData As String
  97.     Dim ordType As Long, cJunk As Long, ft As FILETIME
  98.     Static hKeyPrev As Long, cNameMax As Long
  99.     ' When enumerating, cache required data the first time
  100.     If hKeyPrev <> hKey Or cNameMax = 0 Then
  101.         hKeyPrev = hKey
  102.         GetRegValueNext = _
  103.             RegQueryInfoKey(hKey, sNullStr, cJunk, pNull, _
  104.                             cJunk, cJunk, cJunk, cJunk, _
  105.                             cNameMax, cJunk, cJunk, ft)
  106.         If GetRegValueNext Then Exit Function
  107.     End If
  108.     
  109.     ' Get the value name and type in the first call
  110.     vValue = Empty
  111.     cName = cNameMax + 1
  112.     sName = String$(cName, 0)
  113.     GetRegValueNext = _
  114.         RegEnumValue(hKey, i, sName, cName, _
  115.                      pNull, ordType, pNull, cData)
  116.     If GetRegValueNext Then
  117.         If GetRegValueNext <> ERROR_MORE_DATA Then
  118.             Exit Function
  119.         End If
  120.     End If
  121.     sName = Left$(sName, cName)
  122.     
  123.     ' Handle each type separately
  124.     Select Case ordType
  125.     Case REG_DWORD, REG_DWORD_LITTLE_ENDIAN
  126.         Dim iData As Long
  127.         GetRegValueNext = _
  128.             RegEnumValueInt(hKey, i, sName, cName + 1, _
  129.                             pNull, ordType, iData, cData)
  130.         vValue = iData
  131.         
  132.     Case REG_DWORD_BIG_ENDIAN  ' Unlikely, but you never know
  133.         Dim dwData As Long
  134.         GetRegValueNext = _
  135.             RegEnumValueInt(hKey, i, sName, cName + 1, _
  136.                             pNull, ordType, dwData, cData)
  137.         vValue = MBytes.SwapEndian(dwData)
  138.         
  139.     Case REG_SZ, REG_MULTI_SZ ' Same thing to Visual Basic
  140.         sData = String$(cData - 1, 0)
  141.         GetRegValueNext = _
  142.             RegEnumValueStr(hKey, i, sName, cName + 1, _
  143.                             pNull, ordType, sData, cData)
  144.         vValue = sData
  145.         
  146.     Case REG_EXPAND_SZ         ' Expand environment variables
  147.         sData = String$(cData - 1, 0)
  148.         GetRegValueNext = _
  149.             RegEnumValueStr(hKey, i, sName, cName + 1, _
  150.                             pNull, ordType, sData, cData)
  151.         vValue = MUtility.ExpandEnvStr(sData)
  152.     
  153.     Case Else       ' Catch REG_BINARY and anything else
  154.         Dim abData() As Byte
  155.         ReDim abData(cData)
  156.         GetRegValueNext = _
  157.             RegEnumValueByte(hKey, i, sName, cName + 1, _
  158.                              pNull, ordType, _
  159.                              abData(0), cData)
  160.         vValue = abData
  161.         
  162.     End Select
  163.     
  164. End Function
  165.  
  166. Function GetRegNodeNext(ByVal hKey As Long, i As Long, sName As String) As Long
  167.     Dim cName As Long, cJunk As Long, ft As FILETIME
  168.     Static hKeyPrev As Long, cNameMax As Long
  169.     If hKeyPrev <> hKey Or cNameMax = 0 Then
  170.         hKeyPrev = hKey
  171.         GetRegNodeNext = RegQueryInfoKey(hKey, sNullStr, cJunk, pNull, _
  172.                                          cJunk, cNameMax, cJunk, cJunk, _
  173.                                          cJunk, cJunk, cJunk, ft)
  174.         If GetRegNodeNext Then Exit Function
  175.     End If
  176.     
  177.     cName = cNameMax + 1
  178.     sName = String$(cName, 0)
  179.     GetRegNodeNext = RegEnumKeyEx(hKey, i, sName, cName, _
  180.                                   pNull, sNullStr, cJunk, ft)
  181.     sName = Left$(sName, cName)
  182.    
  183. End Function
  184.  
  185. Function CreateRegNode(ByVal hKey As Long, sKeyNew As String, _
  186.                        hKeyNew As Long, Optional fExisted As Boolean, _
  187.                        Optional ByVal afAccess As Long = KEY_ALL_ACCESS _
  188.                        ) As Long
  189.     Dim e As Long, ordResult As Long
  190.     CreateRegNode = RegCreateKeyEx(hKey, sKeyNew, 0&, sEmpty, _
  191.                                    REG_OPTION_NON_VOLATILE, _
  192.                                    afAccess, pNull, _
  193.                                    hKeyNew, ordResult)
  194.     fExisted = (ordResult = REG_OPENED_EXISTING_KEY)
  195. End Function
  196.  
  197. ' Delete node, but only if it has no subnodes (emulate WinNT RegDeleteKey)
  198. Function DeleteOneRegNode(ByVal hKeyRoot As Long, sKey As String) As Long
  199.     If MUtility.IsNT Then
  200.         DeleteOneRegNode = RegDeleteKey(hKeyRoot, sKey)
  201.     Else
  202.         ' Check to see if there are subnodes
  203.         Dim cJunk As Long, e As Long, cNode As Long, ft As FILETIME
  204.         e = RegQueryInfoKey(hKeyRoot, sNullStr, cJunk, _
  205.                             pNull, cNode, cJunk, cJunk, _
  206.                             cJunk, cJunk, cJunk, cJunk, ft)
  207.         ' Delete only if no nodes
  208.         If cNode = 0 Then
  209.             DeleteOneRegNode = RegDeleteKey(hKeyRoot, sKey)
  210.         Else
  211.             DeleteOneRegNode = ERROR_ACCESS_DENIED
  212.         End If
  213.     End If
  214. End Function
  215.  
  216. ' Delete node and all its subnodes (emulate Win95 RegDeleteKey)
  217. Function DeleteRegNodes(ByVal hKeyRoot As Long, sKey As String) As Long
  218.     Dim sKeyT As String, hSubKey As Long, ft As FILETIME
  219.  
  220.     ' Try to delete whole thing--always works for Win95, but fails on
  221.     ' nodes with subnodes in WinNT
  222.     DeleteRegNodes = RegDeleteKey(hKeyRoot, sKey)
  223.     If DeleteRegNodes = ERROR_SUCCESS Then Exit Function
  224.     DeleteRegNodes = RegOpenKeyEx(hKeyRoot, sKey, 0, _
  225.                                   KEY_ALL_ACCESS, hSubKey)
  226.     ' Delete each subnode
  227.     Do While DeleteRegNodes = ERROR_SUCCESS
  228.         sKeyT = String$(cMaxPath, 0)
  229.         DeleteRegNodes = RegEnumKeyEx(hSubKey, 0, sKeyT, cMaxPath, _
  230.                                       pNull, sNullStr, 0, ft)
  231.         sKeyT = MUtility.StrZToStr(sKeyT)
  232.         ' Recursive call to remove node and any subnodes
  233.         If DeleteRegNodes = ERROR_SUCCESS Then
  234.             DeleteRegNodes = DeleteRegNodes(hSubKey, sKeyT)
  235.         End If
  236.     Loop
  237.     Call RegCloseKey(hSubKey)
  238.     ' Try to delete root again
  239.     DeleteRegNodes = RegDeleteKey(hKeyRoot, sKey)
  240.     
  241. End Function
  242.  
  243. Function GetRegStr(sKey As String, sItem As String, _
  244.                    Optional ByVal hRoot As EROOTKEY _
  245.                        = HKEY_CURRENT_USER) As String
  246.     Dim e As Long, hKey As Long, s As String
  247.     ' Open a subkey
  248.     e = RegOpenKeyEx(hRoot, sKey, 0, KEY_QUERY_VALUE, hKey)
  249.     ApiRaiseIf e
  250.     Dim ert As EREGTYPE, c As Long
  251.     ' Get the length and make sure it's a string
  252.     e = RegQueryValueEx(hKey, sItem, 0&, ert, 0&, c)
  253.     ApiRaiseIf e
  254.     BugAssert ert = REG_SZ
  255.     If c <> 0 Then
  256.         s = String$(c - 1, 0)
  257.         ' Read the string
  258.         e = RegQueryValueExStr(hKey, sItem, 0&, ert, s, c)
  259.         ApiRaiseIf e
  260.     End If
  261.     RegCloseKey hKey
  262.     GetRegStr = s
  263. End Function
  264.  
  265. Function GetRegInt(sKey As String, sItem As String, _
  266.                    Optional ByVal hRoot As EROOTKEY = HKEY_CURRENT_USER _
  267.                    ) As Long
  268.     Dim e As Long, hKey As Long
  269.     ' Open a subkey
  270.     e = RegOpenKeyEx(hRoot, sKey, 0, KEY_QUERY_VALUE, hKey)
  271.     ApiRaiseIf e
  272.     Dim ert As EREGTYPE, iVal As Long, c As Long
  273.     ' Get the length and make sure it's an integer
  274.     e = RegQueryValueEx(hKey, sItem, 0&, ert, 0&, c)
  275.     ApiRaiseIf e
  276.     BugAssert ert = REG_DWORD
  277.     If c <> 0 Then
  278.         ' Read the integer
  279.         e = RegQueryValueExInt(hKey, sItem, 0&, ert, iVal, c)
  280.         ApiRaiseIf e
  281.     End If
  282.     RegCloseKey hKey
  283.     GetRegInt = iVal
  284. End Function
  285.  
  286. ' Get key locations in registry
  287.  
  288. Function GetDesktop() As String
  289.     GetDesktop = GetRegStr(sWinExp, "Desktop") & sBack
  290. End Function
  291.  
  292. Function GetFavorites() As String
  293.     GetFavorites = GetRegStr(sWinExp, "Favorites") & sBack
  294. End Function
  295.  
  296. Function GetStartMenu() As String
  297.     GetStartMenu = GetRegStr(sWinExp, "Start Menu") & sBack
  298. End Function
  299.  
  300. Function GetStartup() As String
  301.     GetStartup = GetRegStr(sWinExp, "Startup") & sBack
  302. End Function
  303.  
  304. Function GetPrograms() As String
  305.     GetPrograms = GetRegStr(sWinExp, "Programs") & sBack
  306. End Function
  307.  
  308. Function GetAppData() As String
  309.     GetAppData = GetRegStr(sWinExp, "AppData") & sBack
  310. End Function
  311.  
  312. Function GetCommonDesktop() As String
  313.     GetCommonDesktop = GetRegStr(sWinExp, "Common Desktop") & sBack
  314. End Function
  315.  
  316. Function GetCommonStartMenu() As String
  317.     GetCommonStartMenu = GetRegStr(sWinExp, "Common Start Menu") & sBack
  318. End Function
  319.  
  320. Function GetCommonStartup() As String
  321.     GetCommonStartup = GetRegStr(sWinExp, "Common Startup") & sBack
  322. End Function
  323.  
  324. Function GetCommonPrograms() As String
  325.     GetCommonPrograms = GetRegStr(sWinExp, "Common Programs") & sBack
  326. End Function
  327.  
  328. #If fComponent = 0 Then
  329. Private Sub ErrRaise(e As Long)
  330.     Dim sText As String, sSource As String
  331.     If e > 1000 Then
  332.         sSource = App.ExeName & ".RegTool"
  333.         Select Case e
  334.         Case eeBaseRegTool
  335.             BugAssert True
  336.        ' Case ee...
  337.        '     Add additional errors
  338.         End Select
  339.         Err.Raise COMError(e), sSource, sText
  340.     Else
  341.         ' Raise standard Visual Basic error
  342.         sSource = App.ExeName & ".VBError"
  343.         Err.Raise e, sSource
  344.     End If
  345. End Sub
  346. #End If
  347.  
  348.